//
//  NewFeatureViewController.swift
//  HYYWeibo
//
//  Created by HY-Mac on 16/7/15.
//  Copyright © 2016年 HY-Mac. All rights reserved.
//

import UIKit
import SnapKit

class NewFeatureViewController: UIViewController {
    private var maxCount = 4
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    
    

}

extension NewFeatureViewController: UICollectionViewDelegate{
    
    func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //HYLog(indexPath.item)
        let index = collectionView.indexPathsForVisibleItems().last!
        HYLog(index.item)
        let currentCell = collectionView.cellForItemAtIndexPath(index) as! HYCollectionViewCell
        if index.item == (maxCount - 1) {
            currentCell.startAniamtion()
        }else{
            
        }
        
        
    }
    
}

extension NewFeatureViewController: UICollectionViewDataSource{
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return maxCount
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("NewFeature", forIndexPath: indexPath) as! HYCollectionViewCell
        //cell.backgroundColor = (indexPath.item % 2 == 0) ? UIColor.redColor(): UIColor.grayColor()
        cell.index = indexPath.item
        return cell
    }
}



class HYCollectionViewCell: UICollectionViewCell {
    
    
    private lazy var iconView = UIImageView()
    private lazy var startBtn: UIButton = {
        let btn = UIButton(imageName: nil, bgImageName: "new_feature_button")
        btn.addTarget(self, action: #selector(HYCollectionViewCell.StartBtnClick), forControlEvents: .TouchUpInside)
        //btn.hidden = true
        return btn
    }()
    
    // MARK: - 外部控制方法
    func startAniamtion()
    {
        startBtn.hidden = false
        // 执行放大动画
        /*
         第一个参数: 动画时间
         第二个参数: 延迟时间
         第三个参数: 震幅 0.0~1.0, 值越小震动越列害
         第四个参数: 加速度, 值越大震动越列害
         第五个参数: 动画附加属性
         第六个参数: 执行动画的block
         第七个参数: 执行完毕后回调的block
         */
        startBtn.transform = CGAffineTransformMakeScale(0.0, 0.0)
        startBtn.userInteractionEnabled = false
        UIView.animateWithDuration(2.0, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startBtn.transform = CGAffineTransformIdentity
            
            }, completion: { (_) -> Void in
                self.startBtn.userInteractionEnabled = true
        })
    }
    var index = 0{
        didSet{
            let name = "new_feature_\(index + 1)"
            iconView.image = UIImage(named: name)
            startBtn.hidden = true
        }
    }
    
    @objc private func StartBtnClick(){
        //HYLog("")
        NSNotificationCenter.defaultCenter().postNotificationName(HYChangeRootViewController, object: false)
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
        
    }
    private func setupUI(){
        contentView.addSubview(iconView)
        contentView.addSubview(startBtn)
        iconView.snp_makeConstraints { (make) in
            //make.center.equalTo(self.contentView.center)
            make.edges.equalTo(0)
        }
        startBtn.snp_makeConstraints { (make) in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-100)
        }
        
    }
    
    
}

class HYCollectionViewLayout: UICollectionViewFlowLayout{
    override func prepareLayout() {
        itemSize = UIScreen.mainScreen().bounds.size
        
        minimumLineSpacing = 0
        minimumLineSpacing = 0
        
        scrollDirection = .Horizontal
        
        collectionView?.pagingEnabled = true
        
        collectionView?.bounces = false
        
        collectionView?.showsVerticalScrollIndicator = false
        collectionView?.showsHorizontalScrollIndicator = false
        
    }
}





